package com.xxbb.chapter_8.dome3;

import java.time.Clock;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket {
    private int ticketNumber = 3;
    private  final Lock LOCK=new ReentrantLock();//锁对象
    public  int sell() {
    LOCK.lock();
//    public synchronized int sell() {//同步方法
        //synchronized (this) {//同步代码块
            try {
                if (ticketNumber > 0) {
                    int num = ticketNumber;
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticketNumber--;
                    return num;
                }
                return 0;
            }finally {
                LOCK.unlock();//释放锁
            }
        //}
    }
}
